package listeners;

import handlers.ClientHandler;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Listener for the Clients
 * 
 * @author Nils Berlijn & Henderikus Harms
 */		
public class ClientListener implements Runnable
{
	private boolean listen;
	private ServerSocket socket;
	private static final int PORT = 6052;
	
	/**
	 * Initialize the ClientListener
	 * Listen to true
	 * Setup the ServerSocket
	 */	
	public ClientListener()
	{
		listen = true;
		
		try 
		{
			this.socket = new ServerSocket(PORT);
		} 
		catch(IOException e) 
		{
			e.printStackTrace();
		}
	}

	/**
	 * Run the Client Listener
	 * 
	 * @param clientSocket Accept the socket
	 * @param thread Make a Thread that will handle the incoming connections
	 */
	@Override
	public void run() 
	{
		System.out.println("Client Listener running");
		
		Socket clientSocket;
		
		while(listen)
		{
			try 
			{
				clientSocket = socket.accept();
				
				Thread thread = new Thread(new ClientHandler(clientSocket));
				thread.start();
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
		}
	}
}
